{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 元组 `tuple`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 元组是一个不可变的序列\n",
    "- 它的操作的方式基本上和列表是一样的\n",
    "- 操作元组时将元组当成是一个不可变的列表就 OK 了\n",
    "- 当我们希望数据不改变时，就使用元组，其余情况都使用列表"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 元组的创建\n",
    "\n",
    "- 使用 `()` 来创建元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_tuple = () # 创建了一个空元组\n",
    "print(f'my_tuple = {my_tuple}, type = {type(my_tuple)}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_tuple = (1,2,3,4,5)\n",
    "print(f'my_tuple = {my_tuple}, type = {type(my_tuple)}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 元组是不可变对象，不能尝试为元组中的元素重新赋值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_tuple = (1,2,3,4,5)\n",
    "my_tuple[1] = 1\n",
    "print(f'my_tuple = {my_tuple}, type = {type(my_tuple)}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 当元组不是空元组时，括号可以省略"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_tuple = 1,2,3,4,5\n",
    "print(f'my_tuple = {my_tuple}, type = {type(my_tuple)}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 当元组不是空元组时，至少要有一个 `,`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_tuple = 1, # tuple\n",
    "print(f'my_tuple = {my_tuple}, type = {type(my_tuple)}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_tuple = (1,) # tuple\n",
    "print(f'my_tuple = {my_tuple}, type = {type(my_tuple)}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_tuple = (1) # int\n",
    "print(f'my_tuple = {my_tuple}, type = {type(my_tuple)}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_tuple = 1 # int\n",
    "print(f'my_tuple = {my_tuple}, type = {type(my_tuple)}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 解构赋值\n",
    "\n",
    "- 解构（解包）就是将元组中的每一个元素都赋值给一个变量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_students = ('Issac','Qihan Wu','Acker','Derick')\n",
    "\n",
    "student_1,student_2,student_3,student_4 = my_students\n",
    "\n",
    "print(f'student_1 = {student_1}')\n",
    "print(f'student_2 = {student_2}')\n",
    "print(f'student_3 = {student_3}')\n",
    "print(f'student_4 = {student_4}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 可以通过利用元组的解包交换两个（或多个）变量的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 2333\n",
    "b = 8848\n",
    "\n",
    "print(f'a = {a}, b = {b}')\n",
    "\n",
    "a, b = b, a\n",
    "\n",
    "print(f'a = {a}, b = {b}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 在对一个元组进行解包时，变量的数量必须和元组中元素的数量一致\n",
    "- 也可以在变量前边添加一个 `*` ，这样变量将会获取元组中所有剩余元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_tuple = 1,2,3,4,5,6,7,8,9,10\n",
    "\n",
    "a, b, c, *d = my_tuple\n",
    "\n",
    "print(f'a = {a}')\n",
    "print(f'b = {b}')\n",
    "print(f'c = {c}')\n",
    "print(f'd = {d}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_tuple = 1,2,3,4,5,6,7,8,9,10\n",
    "\n",
    "a, b, *c, d = my_tuple\n",
    "\n",
    "print(f'a = {a}')\n",
    "print(f'b = {b}')\n",
    "print(f'c = {c}')\n",
    "print(f'd = {d}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 列表和字符串也可以解包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a, b, *c, d = 'Hello World'\n",
    "\n",
    "print(f'a = {a}')\n",
    "print(f'b = {b}')\n",
    "print(f'c = {c}')\n",
    "print(f'd = {d}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a, b, *c, d = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n",
    "\n",
    "print(f'a = {a}')\n",
    "print(f'b = {b}')\n",
    "print(f'c = {c}')\n",
    "print(f'd = {d}')"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "e7370f93d1d0cde622a1f8e1c04877d8463912d04d973331ad4851f04de6915a"
  },
  "kernelspec": {
   "display_name": "Python 3.9.4 64-bit",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.4"
  },
  "orig_nbformat": 2
 },
 "nbformat": 4,
 "nbformat_minor": 2
}